﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace PIMS.Utils
{
    internal class FileChanged
    {
        string file;
        string fileLock;

        public FileChanged(string filename) 
        {
           this.file = filename;
           this.fileLock = filename+".lastEdit";
        }

        public bool IsFileChanged()
        {
            try
            {
                if (File.Exists(this.file))
                {
                    FileInfo fi = new FileInfo(this.file);
                    if (!File.Exists(this.fileLock))
                    {
                        File.WriteAllText(this.fileLock, fi.LastWriteTime.ToString("yyyy-MM-dd HH:mm:ss"));
                        return true;
                    }
                    else
                    {
                        string lastWriteTime = File.ReadAllText(this.fileLock);
                        if (fi.LastWriteTime.ToString("yyyy-MM-dd HH:mm:ss") != lastWriteTime)
                        {
                            File.WriteAllText(this.fileLock, fi.LastWriteTime.ToString("yyyy-MM-dd HH:mm:ss"));
                            return true;
                        }
                    }
                }
            }
            catch
            { 
            }
            return false;
        }
    }
}
